package com.app.activitys;

import com.app.entidades.Album;
import com.app.entidades.Bandas;
import com.app.entidades.Bar;
import com.app.entidades.Coletania;
import com.app.entidades.FaderDir;
import com.app.entidades.FaderEsq;
import com.app.entidades.Show;
import com.app.entidades.Musicas;
import com.app.entidades.Videos;
import com.app.webservice.AlbumRest;
import com.app.webservice.BandasRest;
import com.app.webservice.BarRest;
import com.app.webservice.ColetaniaRest;
import com.app.webservice.ShowRest;
import com.app.webservice.MusicaRest;
import com.app.webservice.VideoRest;
import com.example.applucianokikao.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Process;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.os.AsyncTask;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;



public class ActivityMenu extends Activity{

	private Intent intent;
	private Handler handler = new Handler();
	private LinearLayout menu1;
	private LinearLayout menu2;
	private LinearLayout menu3;
	private Context contexto;
	private Dialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		

		contexto = this;
		menu1 = (LinearLayout)findViewById(R.id.linear_primeiro);
		menu2 = (LinearLayout)findViewById(R.id.linear_segundo);
		menu3 = (LinearLayout)findViewById(R.id.linear_ultimo);
		
		ImageButton button_composi = (ImageButton) findViewById(R.id.button_composicoes);
		ImageButton button_bandas = (ImageButton) findViewById(R.id.button_bandas_variadas);
		ImageButton button_coletanias = (ImageButton) findViewById(R.id.button_coletanias);
		ImageButton button_galeria = (ImageButton) findViewById(R.id.button_galeria);
		ImageButton button_videos = (ImageButton) findViewById(R.id.button_videos);
		ImageButton button_bares = (ImageButton) findViewById(R.id.button_bares);
		ImageButton button_show = (ImageButton) findViewById(R.id.button_shows);
		ImageButton button_contato = (ImageButton) findViewById(R.id.button_contato);
		
		
		ImageButton button_facebook = (ImageButton) findViewById(R.id.button_facebook);
		ImageButton button_tw = (ImageButton) findViewById(R.id.button_tw);
		ImageButton button_youtube = (ImageButton) findViewById(R.id.button_youtube);
		ImageButton button_instagram = (ImageButton) findViewById(R.id.button_instagram);
		
		
		final ImageButton button_direita = (ImageButton) findViewById(R.id.button_seta_dir);
		final ImageButton button_esquerda = (ImageButton) findViewById(R.id.button_seta_esq);
		button_esquerda.setEnabled(false);
		
		
		button_composi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				AsyncTask<Void, Void,List<Musicas>> task = new AsyncTask<Void,Void,List<Musicas>>(){
					
					@Override
					protected void onPreExecute(){
						dialog = criarDialog();
						dialog.setTitle("Carregando Composições");
						dialog.show();
					}
					
					@Override
					protected List<Musicas> doInBackground(Void... arg0) {
						
						MusicaRest rest = new MusicaRest();
						List<Musicas> musicas = new ArrayList<Musicas>();
						try{
							musicas = rest.getListaMusicas();
						}catch (Exception e) {
							intent = new Intent(ActivityMenu.this, ActivityProblemaInternet.class);
							dialog.dismiss();
							startActivity(intent);
							this.cancel(true);
						}
						return musicas;
					}
					
					@Override
			        protected void onPostExecute(List<Musicas> result) {
						
						Bundle bundle = new Bundle();
						bundle.putSerializable("composicoes", (Serializable)result);
						
						intent = new Intent(ActivityMenu.this, PlayerComposicoes.class);
						intent.putExtra("compo", bundle);
						intent.putExtra("activity", "main");
						dialog.dismiss();
						startActivity(intent);				
			        }
					
				};
				task.execute();
				
			}
		});
	
		button_bandas.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AsyncTask<Void, Void,List<Bandas>> task = new AsyncTask<Void,Void,List<Bandas>>(){

					@Override
					protected void onPreExecute(){
						dialog = criarDialog();
						dialog.setTitle("Carregando Bandas");
						dialog.show();
					}
		
					@Override
					protected List<Bandas> doInBackground(Void... params) {
				
						BandasRest rest = new BandasRest();
						List<Bandas> bandas = new ArrayList<Bandas>();
						
						try{
							bandas = rest.getListaBandas();
						}catch (Exception e) {
							intent = new Intent(ActivityMenu.this, ActivityProblemaInternet.class);
							dialog.dismiss();
							startActivity(intent);
							this.cancel(true);
						}
						return bandas;
					}
					
					@Override
			        protected void onPostExecute(List<Bandas> result) {
						Bundle bundle = new Bundle();
						bundle.putSerializable("bandas", (Serializable)result);
						intent = new Intent(ActivityMenu.this, BandasActivity.class);
						intent.putExtra("ban", bundle);
						dialog.dismiss();
						startActivity(intent);	
					}

				};
				task.execute();	
			}
		});
		
	  button_coletanias.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {

			AsyncTask<Void, Void,List<Coletania>> task = new AsyncTask<Void,Void,List<Coletania>>(){

			@Override
			protected void onPreExecute(){
				dialog = criarDialog();
				dialog.setTitle("Carregando Coletâneas");
				dialog.show();
			}
		
			@Override
			protected List<Coletania> doInBackground(Void... params) {
		
				ColetaniaRest rest = new ColetaniaRest();
				List<Coletania> coletanias = new ArrayList<Coletania>();
				
				try{
					coletanias = rest.getListaColetania();
				}catch (Exception e) {
					intent = new Intent(ActivityMenu.this, ActivityProblemaInternet.class);
					dialog.dismiss();
					startActivity(intent);
					this.cancel(true);
				}
				return coletanias;
			}
			
			@Override
	        protected void onPostExecute(List<Coletania> result) {
				Bundle bundle = new Bundle();
				bundle.putSerializable("coletanias", (Serializable)result);
				intent = new Intent(ActivityMenu.this, ActivityColetania.class);
				intent.putExtra("cole", bundle);
				dialog.dismiss();
				startActivity(intent);	
			}

		};
		task.execute();	

		}
	});	
		
		
	  button_galeria.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			AsyncTask<Void, Void,List<Album>> task = new AsyncTask<Void,Void,List<Album>>(){

				@Override
				protected void onPreExecute(){
					dialog = criarDialog();
					dialog.setTitle("Carregando Galeria");
					dialog.show();
				}
				
				@Override
				protected List<Album> doInBackground(Void... arg0) {
					AlbumRest rest = new AlbumRest();
					List<Album> listaAlbum = new ArrayList<Album>();
					try{
						listaAlbum = rest.getListaAlbuns();
					}catch (Exception e) {
						intent = new Intent(ActivityMenu.this, ActivityProblemaInternet.class);
						dialog.dismiss();
						startActivity(intent);
						this.cancel(true);
					}
					return listaAlbum;
				}

				@Override
		        protected void onPostExecute(List<Album> result) {
			
					Bundle bundle = new Bundle();
					bundle.putSerializable("albuns", (Serializable)result);
					intent = new Intent(ActivityMenu.this, GaleriaActivity.class);
					intent.putExtra("alb", bundle);
					dialog.dismiss();
					startActivity(intent);	
				};
				
			};
			task.execute();
		}
	  });
		
	  button_videos.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				AsyncTask<Void, Void,List<Videos>> task = new AsyncTask<Void,Void,List<Videos>>(){

					@Override
					protected void onPreExecute(){
						dialog = criarDialog();
						dialog.setTitle("Carregando Videos");
						dialog.show();
					}
					
					@Override
					protected List<Videos> doInBackground(Void... arg0) {
						VideoRest rest = new VideoRest();
						List<Videos> listaVideos = new ArrayList<Videos>();
						try{
							listaVideos = rest.getListaVideos();
						}catch (Exception e) {
							intent = new Intent(ActivityMenu.this, ActivityProblemaInternet.class);
							dialog.dismiss();
							startActivity(intent);
							this.cancel(true);
						}
						return listaVideos;
					}

					@Override
			        protected void onPostExecute(List<Videos> result) {
						Bundle bundle = new Bundle();
						bundle.putSerializable("videos", (Serializable)result);
						intent = new Intent(ActivityMenu.this, VideoActiv.class);
						intent.putExtra("vid", bundle);
						dialog.dismiss();
						startActivity(intent);	
					}
					
				};
					task.execute();		
	    
			}
		});
	
	  button_bares.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				AsyncTask<Void, Void,List<Bar>> task = new AsyncTask<Void,Void,List<Bar>>(){

					@Override
					protected void onPreExecute(){
						dialog = criarDialog();
						dialog.setTitle("Carregando Bares");
						dialog.show();
					}
					
					@Override
					protected List<Bar> doInBackground(Void... arg0) {
						BarRest rest = new BarRest();
						List<Bar> listaBar = new ArrayList<Bar>();
						try{
							//if(TemConexao()){
								listaBar = rest.getListaBar();
							//}
							//implementar a parte do banco aqui
							
						}catch (Exception e) {
							intent = new Intent(ActivityMenu.this, ActivityProblemaInternet.class);
							dialog.dismiss();
							startActivity(intent);
							this.cancel(true);
						}
						return listaBar;
					}

					@Override
			        protected void onPostExecute(List<Bar> result) {
						Bundle bundle = new Bundle();
						bundle.putSerializable("bares", (Serializable)result);
						intent = new Intent(ActivityMenu.this, ActivityBares.class);
						intent.putExtra("bar", bundle);
						dialog.dismiss();
						startActivity(intent);	
					}
					
				};
					task.execute();		
				
				
			}
		});
	  
	button_show.setOnClickListener(new OnClickListener() {
		
		@Override
		public void onClick(View v) {
		
			AsyncTask<Void, Void,List<Show>> task = new AsyncTask<Void,Void,List<Show>>(){

				@Override
				protected void onPreExecute(){
					dialog = criarDialog();
					dialog.setTitle("Carregando Shows");
					dialog.show();
				}
				
				@Override
				protected List<Show> doInBackground(Void... arg0) {
					ShowRest rest = new ShowRest();
					List<Show> listaShow = new ArrayList<Show>();
					try{
						listaShow = rest.getListaShows();
					}catch (Exception e) {
						intent = new Intent(ActivityMenu.this, ActivityProblemaInternet.class);
						dialog.dismiss();
						startActivity(intent);
						this.cancel(true);
					}
					return listaShow;
				}

				@Override
		        protected void onPostExecute(List<Show> result) {
					System.out.println(result.isEmpty());
					Bundle bundle = new Bundle();
					bundle.putSerializable("shows", (Serializable)result);
					intent = new Intent(ActivityMenu.this, ActivityShow.class);
					intent.putExtra("show", bundle);
					dialog.dismiss();
					startActivity(intent);	
				}
				
			};
				task.execute();		

		}
	});
	  
		
	button_contato.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				intent = new Intent(ActivityMenu.this, ActivityContato.class);
				startActivity(intent);
			}
	});
	 
	  
	button_direita.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				if(menu1.getVisibility()==View.VISIBLE){
					FaderDir.runAlphaAnimation((Activity) contexto, R.id.linear_primeiro,1);
					menu1.setVisibility(View.INVISIBLE);
					menu2.setVisibility(View.VISIBLE);
					FaderDir.runAlphaAnimation((Activity) contexto, R.id.linear_segundo,0);
					menu3.setVisibility(View.INVISIBLE);
					button_esquerda.setEnabled(true);
					button_direita.setEnabled(true);
				}else if(menu2.getVisibility()==View.VISIBLE){
					FaderDir.runAlphaAnimation((Activity) contexto, R.id.linear_segundo,1);
					menu2.setVisibility(View.INVISIBLE);
					menu3.setVisibility(View.VISIBLE);
					FaderDir.runAlphaAnimation((Activity) contexto, R.id.linear_ultimo,0);
					menu1.setVisibility(View.INVISIBLE);
					button_direita.setEnabled(false);
					button_esquerda.setEnabled(true);
				}
				
			}
	});

	button_esquerda.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				if(menu2.getVisibility()==View.VISIBLE){
					FaderEsq.runAlphaAnimation((Activity) contexto, R.id.linear_segundo,1);
					menu2.setVisibility(View.INVISIBLE);
					menu1.setVisibility(View.VISIBLE);
					FaderEsq.runAlphaAnimation((Activity) contexto, R.id.linear_primeiro,0);
					menu3.setVisibility(View.INVISIBLE);
					button_esquerda.setEnabled(false);
					button_direita.setEnabled(true);
				}else if(menu3.getVisibility()==View.VISIBLE){
					FaderEsq.runAlphaAnimation((Activity) contexto, R.id.linear_ultimo,1);
					menu3.setVisibility(View.INVISIBLE);
					menu2.setVisibility(View.VISIBLE);
					FaderEsq.runAlphaAnimation((Activity) contexto, R.id.linear_segundo,0);
					menu1.setVisibility(View.INVISIBLE);
					button_esquerda.setEnabled(true);
					button_direita.setEnabled(true);
				}				
			}
	});
		
		button_facebook.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("https://www.facebook.com/lucianokikaooficial?fref=ts");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});
		
		button_tw.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("https://twitter.com/lucianokikao");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});
		
		button_youtube.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("http://www.youtube.com/lucianokikaovideos");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});
		
		button_instagram.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Uri uri = Uri.parse("http://instagram.com/lucianokikao#");
				Intent intent = new Intent(Intent.ACTION_VIEW, uri);
				startActivity(intent);
			}
		});
		
	}
	
	@Override
	protected void onDestroy() {
		System.exit(0);
		super.onDestroy();
	}
	
	private boolean TemConexao(){ 
	     boolean lblnRet = false;
	     try
	     {
	          ConnectivityManager cm = (ConnectivityManager)
	          getSystemService(Context.CONNECTIVITY_SERVICE); 
	          if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isAvailable() && cm.getActiveNetworkInfo().isConnected()) { 
	              lblnRet = true; 
	          } else { 
	              lblnRet = false; 
	          }
	     }catch (Exception e) {
	          e.printStackTrace();
	     }
	      return lblnRet;
	}
	
	public Dialog criarDialog(){
		Dialog di = new Dialog(contexto,R.style.Theme_Dialog_Translucent);
        di.setCancelable(false);
        di.setContentView(R.layout.theme);
		return di;
	}
}
